﻿using System;

namespace N2.Workflow.Items
{
	using Details;
	using Integrity;

	[Definition]
	[WithEditableName("Name", 01), WithEditableTitle]
	[RestrictParents(typeof(StateDefinition))]
	public class ActionDefinition :
		ContentItem,
		IActionDefinition
	{
		#region Fields
		
		//readonly PersistingTypePropertyHolder StateTypeHolder;

		#endregion Fields

		#region Constructor

		public ActionDefinition()
		{
			//this.StateTypeHolder = new PersistingTypePropertyHolder(this, "StateTypeName", typeof(ItemState));
		}

		#endregion Constructor

		#region Business properties

		[EditableLink("Destination State", 13, Required = true)]
		public StateDefinition LeadsTo {
			get { return this.GetDetail("LeadsTo") as StateDefinition; }
			set { this.SetDetail<StateDefinition>("LeadsTo", value); }
		}

		internal StateDefinition StateDefinition {
			get { return this.Parent as StateDefinition; }
		}
		
		#endregion Business properties

		#region System properties

		public override bool IsPage { get { return false; } }

		public override string Title {
			get { return base.Title ?? this.GetDefaultTitle(); }
		}

		string GetDefaultTitle()
		{
			string _result = this.Name;

			if (null != this.LeadsTo) {
				_result = string.Concat(_result, "&mdash;", this.LeadsTo.Title);
			}

			return _result;
		}

		public override string IconUrl {
			get {
				return 0 == this.ID
					? Constants.IconPath + "workflow_actions.png"
					: this.LeadsTo.IconUrl;
			}
		}

		public override string TemplateUrl { get { return "~/Templates/UI/Parts/Empty.ascx"; } }

		public override string ZoneName {
			get { return base.ZoneName ?? "SiteRight"; }
		}

		#endregion System properties

		#region IActionDefinition Members

		IStateDefinition IActionDefinition.StateDefinition
		{
			get { return this.StateDefinition as IStateDefinition; }
		}

		IStateDefinition IActionDefinition.LeadsTo
		{
			get { return this.LeadsTo as IStateDefinition; }
		}

		#endregion

		#region IWorkflowDefinitionItem Members

		IWorkflowDefinition IWorkflowDefinitionItem.Workflow {
			get { return (this as IActionDefinition).StateDefinition.Workflow; }
		}

		#endregion
	}
}
